<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * Controller for managing modules.
 * @package backend-modules/conrollers
 * @category Controller
 * 
 * @author Mark Angelo Angulo
 * @copyright (c) 2015
 */
class Module extends Backend_Controller {

    /**
     *  Constructor method.
     */
    function __construct()
    {
        parent::__construct();
        
        $this->load->model('module_model');
        
        $this->load->helper('cms');
        $this->load->helper('module');
        
        $this->load->library('form_validation');
        $this->form_validation->set_error_delimiters('<div class="clear-fix"></div><small>', '</small>');
        
        init_module();
    }

    /**
     * Default method called when nothing is specified.
     * Displays dashboard.
     * @access public
     */
    public function index()
    {
        view_modules_list();
    }
    
    /*
     * Method called for adding a new record
     * @access public
     */
    public function add()
    {

        if( isset($_POST) && !empty($_POST) )
        {
            if ( valid_add_module() )
            {

                $function_call = array();
                $permissions    = $this->input->post('permission');
                $function_calls = $this->input->post('function_call');
                
                if( set_value('permission[]') && set_value('function_call[]') )
                {
                    for( $i=0; $i < count($permissions); $i++ )
                    {
                        $function_call[ $permissions[$i] ] = $function_calls[$i];
                    }
                }
                
                $module = array();
                
                $module['permissions']    = serialize( $permissions );
                $module['function_call']  = serialize( $function_call );
                
                set_value('name')               ? $module['name']           = clean_text( set_value('name') )               : '';
                set_value('description')        ? $module['description']    = clean_text( set_value('description') )        : '';
                set_value('status')             ? $module['status']         = clean_text( set_value('status') )             : 'active';
                
                $this->module_model->insert($module);

                // add another
                if( $this->input->post('add-another') )
                {
                    $this->form_validation->unset_field_data();
                    view_module_form('Add Module', 'add_success');
                } else
                {
                    view_modules_list(NULL, 'add_success');
                }
                
            } else
            {
                view_module_form('Add Module', 'add_failed');
            }
            
        } else
        {
            view_module_form('Add Module');
        }
    }
    
    /**
     * Method called for editing a new record.
     * @access public
     */
    public function edit()
    {
        $params = $this->uri->uri_to_assoc();

        if( isset($_POST) && ! empty($_POST) )
        {
            if ( valid_edit_module() )
            {
                $module = array();
                set_value('name')           ? $module['name']           = clean_text( set_value('name') )               : '';
                set_value('description')    ? $module['description']    = clean_text( set_value('description') )        : '';
                set_value('permission[]')   ? $module['permissions']    = serialize( $this->input->post('permission') ) : '';
                set_value('status')         ? $module['status']         = clean_text( set_value('status') )             : '';
                $module['date_modified']    = date('Y-n-d H:i:s');
                
                $this->module_model->update( array('id' => set_value('id')), $module);
                view_modules_list(NULL, 'edit_success');
            } else
            {
                $module = $this->module_model->find(set_value('id'))->get();
                view_module_form('Edit Module', 'edit_failed', $module);
            }
        } else
        {
            if( isset($params['id']) && valid_get_id($params['id']) )
            {
                $module = $this->module_model->find($params['id'])->get();
                view_module_form('Edit Module', NULL, $module);
            } else
            {
                $this->show_404();
            }
        }
    }
    
    /**
     * Method called when deleting a record.
     * @access public
     */
    public function delete()
    {
        $params = $this->uri->uri_to_assoc();
        if( isset($params['id']) && valid_get_id($params['id']) )
        {
            // delete from database
            $deleted = $this->module_model->delete(array('id' => $params['id']));
            if($deleted === 'deleted')
            {
                view_modules_list(NULL, 'delete_success');
            } else
            {
                view_modules_list(NULL, 'archived');
            }
        } else
        {
            $this->show_404();
        }
    }
    
    /*
     * Preview information about a record
     * @access public
     */
    public function preview()
    {
        $params = $this->uri->uri_to_assoc();
        if( isset($params['id']) && valid_get_id($params['id']) )
        {
            $filters = array();
            $filters['module'] = $params['id'];
            $this->session->set_userdata('filters', $filters);
            
            view_module_preview($params['id']);
        } else
        {
            $this->show_404();
        }
    }
    
    /**
     * Method called when deleting multiple modules.
     * @access public
     */
    public function delete_multiple()
    {        
        if(valid_ids())
        {
            $items = $this->input->post('items');
            $archived = $this->module_model->until_it_sleeps($items);
            
            if(count($archived) > 0)
            {
                view_list(NULL, 'archived');
            } else
            {
                view_list(NULL, 'delete_success');
            }
        } else
        {
            $this->show_404();
        }
    }
    
}

/* End of file module.php */
/* Location: ./application/modules/backend-modules/controllers/module.php */